package com.woniu.idearbac.security;

import com.woniu.idearbac.Service.PermissionService;
import com.woniu.idearbac.Service.UserService;
import com.woniu.idearbac.entity.SysUser;
import com.woniu.idearbac.mapper.SysRoleMapper;
import com.woniu.idearbac.vo.PermissionVo;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 功能描述
 *
 * @author PC
 * @date -2022/12/1-时间-18:21
 * @product idea-rbac
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserService userService;
    @Resource
    private PermissionService permissionService;
    @Resource
    private SysRoleMapper roleMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        SysUser sysUser = userService.selByTel(username);
        if (sysUser == null) {
            throw new UsernameNotFoundException("登录用户：" + username + " 不存在");
        }
        List<String> permissionlist = permissionService.selectPermissionByTel(username);
        List<String> roles = roleMapper.selectRolesByTel(username);
        List<PermissionVo> vos = permissionService.selMenu(username, 0);
        LoginUser user = new LoginUser();
        user.setUser(sysUser);
        user.setMenuList(vos);
        user.setPermissions(permissionlist);
        user.setRoles(roles);
        return user;
    }
}
